//
//  UILabel+AFF.m
//  AnyfishApp
//
//  Created by Bob Lee on 15/10/25.
//  Copyright © 2015年 Anyfish. All rights reserved.
//

#import "UILabel+AFF.h"

@implementation UILabel (AFF)


- (void)setDataText:(AFFDataText *)dataText{
    NSAttributedString *string = [self attributedStringWithDataText:dataText];
    self.attributedText = string;
}



- (void)verticalUpAlignmentWithText:(NSString *)text maxHeight:(CGFloat)maxHeight
{
    CGRect frame = self.frame;
    CGSize size = [text sizeWithFont:self.font constrainedToSize:CGSizeMake(frame.size.width, maxHeight)];
    frame.size = CGSizeMake(frame.size.width, size.height);
    self.frame = frame;
    self.text = text;
}

- (NSAttributedString *)attributedStringWithDataText:(AFFDataText*)dataText {
    if(dataText && dataText.array && dataText.array.count>0){
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[dataText showText]];
        
        if(!dataText.hideSpecial){
            for(AFFDataPartion *partion in dataText.array){
                if(partion.color){
                    [str addAttribute:NSForegroundColorAttributeName value:partion.color range:partion.range];
                }else if(dataText.color){
                    [str addAttribute:NSForegroundColorAttributeName value:dataText.color range:partion.range];
                }
                
                if(partion.fontSize>0){
                    [str addAttribute:NSFontAttributeName value:[UIFont fontWithSize_M:partion.fontSize] range:partion.range];
                }else if(partion.font){
                    [str addAttribute:NSFontAttributeName value:partion.font range:partion.range];
                }else if(dataText.font){
                    [str addAttribute:NSFontAttributeName value:dataText.font range:partion.range];
                }
            }
        }
        
        return str;
    }
    return nil;
}
@end
